//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime.Tracking;
using Microsoft.Research.eResearch.Ontology.Lookup;
using Microsoft.Research.eResearch.Blackboard.Common;
using System.Globalization;
namespace Microsoft.Research.eResearch.Ontology.Mapping.Mappers
{
    /// <summary>
    /// Implementation for WorkflowTrackingRecord mapping mediator.
    /// </summary>
    [OntologicalTypeMapper(typeof(WorkflowTrackingRecord))]
    public class WorkflowTrackingRecordMapper : IOntologicalTypeMapper
    {
        /// <summary>
        /// Creates list of all the concept value pairs for the given object using lookup table.
        /// </summary>
        /// <param name="table">Instance of the lookup table.</param>
        /// <param name="input">Instance of the input object.</param>
        /// <returns>List of concept value pairs.</returns>
        public List<List<ConceptValuePair>> MapConceptValuePairs(IOntologyLookupTable table, object input)
        {
            List<List<ConceptValuePair>> messages = new List<List<ConceptValuePair>>();

            WorkflowTrackingRecord wtr = input as WorkflowTrackingRecord;
            if (wtr != null)
            {
                List<ConceptValuePair> message = new List<ConceptValuePair>();

                if (wtr.TrackingWorkflowEvent == TrackingWorkflowEvent.Exception)
                {
                    if (wtr.EventArgs != null &&
                        wtr.EventArgs is TrackingWorkflowExceptionEventArgs)
                    {
                        TrackingWorkflowExceptionEventArgs args = wtr.EventArgs as TrackingWorkflowExceptionEventArgs;
                        if (0 != BlackBoardSettings.ExceptionSettings.IsDetailsIncluded(ExceptionDetails.ExceptionType))
                            message.Add(new ConceptValuePair("trident:ExceptionType", args.Exception.GetType().ToString()));
                        if (0 != BlackBoardSettings.ExceptionSettings.IsDetailsIncluded(ExceptionDetails.ExceptionMessage))
                            message.Add(new ConceptValuePair("trident:ExceptionMessage", args.Exception.Message));
                        if (0 != BlackBoardSettings.ExceptionSettings.IsDetailsIncluded(ExceptionDetails.StackTrace))
                            message.Add(new ConceptValuePair("trident:StackTrace", args.Exception.StackTrace));
                    }
                }

                // Properties from TrackingRecord
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(WorkflowTrackingRecord), "EventDateTime"),
                    wtr.EventDateTime.ToUniversalTime().Ticks.ToString()));
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(WorkflowTrackingRecord), "EventOrder"),
                    wtr.EventOrder.ToString(CultureInfo.CurrentCulture)));

                // Properties from WorkflowTrackingRecord
                message.Add(new ConceptValuePair(
                    table.MapPropertyToConcept(typeof(WorkflowTrackingRecord), "TrackingWorkflowEvent"),
                    wtr.TrackingWorkflowEvent.ToString()));

                messages.Add(message);
            }

            return messages;
        }
    }
}
